package com.moneycollect.webspider.common;

import com.moneycollect.webspider.atome.entity.BrandResponse;
import com.moneycollect.webspider.atome.entity.ContactEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author xuhuiqian
 * @since 2021/11/30 14:22
 */
public class HandlerContact {

    private static final Pattern phonePattern = Pattern.compile("([(]{1}\\d{1,3}[)]{1}){1}\\s*[0-9]{4}\\s*[-]{0,1}[0-9]{4}");
    private static final Pattern phonePattern2 = Pattern.compile("[0-9]{3}\\s*[-\\s]{1}[0-9]{4}\\s*[-]{0,1}[0-9]{4}");
    private static final Pattern phonePattern3 = Pattern.compile("\\+[0-9]{1,3}\\s*[-\\s]{1}[0-9]{3}\\s*[-]{0,1}[0-9]{3}\\s*[-]{0,1}[0-9]{4}");
    private static final Pattern emailPattern = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+");
    private static final List<String> invalidEmailList = List.of("someone@example.com", "example@domain.com",
            "email@example.com", "select2@4.1.0-beta.1");
    private static final List<String> invalidEmailEndList = List.of(".jpg", ".jpeg", ".png", ".webp", ".gif");

    public static ContactEntity getContactContent(String htmlContent) {
        var result = new ContactEntity();
        result.setEmail(new ArrayList<>());
        result.setPhone(new ArrayList<>());
        if(StringUtils.isBlank(htmlContent)) {
            return result;
        }

        var matcherPhone1 = phonePattern.matcher(htmlContent);
        while (matcherPhone1.find()) {
            result.getPhone().add(matcherPhone1.group(0));
        }

        var matcherPhone2 = phonePattern2.matcher(htmlContent);
        while (matcherPhone2.find()) {
            result.getPhone().add(matcherPhone2.group(0));
        }

        var matcherPhone3 = phonePattern3.matcher(htmlContent);
        while (matcherPhone3.find()) {
            result.getPhone().add(matcherPhone3.group(0));
        }

        var emailMatcher = emailPattern.matcher(htmlContent);
        while (emailMatcher.find()){
            var mail = emailMatcher.group(0);
            if(invalidEmailList.contains(mail)){
                continue;
            }

            boolean valid = true;
            for(var item : invalidEmailEndList){
                if(mail.toLowerCase().endsWith(item)){
                    valid = false;
                    break;
                }
            }

            if(valid) {
                var matcher = invalidEmailPattern.matcher(mail);
                if(matcher.find()) {
                    valid = false;
                }
            }

            if(valid) {
                result.getEmail().add(mail);
            }
        }

        return result;
    }

    public static void filterEmail(BaseInfoEntity brand) {
        if(brand == null) {
            return;
        }

        if(CollectionUtils.isEmpty(brand.getEmail())) {
            return;
        }

        List<String> newResult = new ArrayList<>();
        for(var email : brand.getEmail()){
            var matcher = invalidEmailPattern.matcher(email);
            if(!matcher.find() && !newResult.contains(email) && !invalidEmailList.contains(email)) {
                boolean valid = true;
                for(var item : invalidEmailEndList){
                    if(email.toLowerCase().endsWith(item)){
                        valid = false;
                        break;
                    }
                }
                if(valid) {
                    newResult.add(email);
                }
            }
        }

        brand.setEmail(newResult);
    }

    public static void filterPhone(BaseInfoEntity brand) {
        if(brand == null) {
            return;
        }

        if(CollectionUtils.isEmpty(brand.getPhone())) {
            return;
        }

        List<String> newResult = new ArrayList<>();
        for(var phone : brand.getPhone()){
            if(!newResult.contains(phone)) {
                newResult.add(phone);
            }
        }

        brand.setPhone(newResult);
    }


    private static final Pattern invalidEmailPattern = Pattern.compile("@\\d+.\\d+.\\d+$");
}
